'----------------------------------------------
' Name: Ali Karbassi & Mike Raasch
' Date: April 10, 2007
' Description: Project 6
'              Form frmCreate
'----------------------------------------------
Public Class frmCreate

   Dim q As UWPCS3340.IQuestion = Nothing

   ''' <summary>
   ''' Sets the buttons to the beginning state.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub frmCreate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      cmdRemove.Enabled = False
      cmdSave.Enabled = False
      setScrollButtons()

   End Sub

   ''' <summary>
   ''' Hides the current form. Shows the start form. Removes all the questions from the form.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub cmdBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBack.Click

      Me.Hide()
      frmStart.Show()

      If questions.Count > 0 Then
         q.Remove(Me)
      End If

   End Sub

   ''' <summary>
   ''' Sets the buttons, creates a new user control, sets the settings, and displays it.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub cmdNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNew.Click

      cmdBack.Enabled = False
      cmdNew.Enabled = False
      cmdSave.Enabled = True
      cmdRemove.Enabled = True

      If questions.Count > 0 Then
         q.Remove(Me)
      End If

      If rdoTrueFalse.Checked = True Then
         q = New UWPCS3340.TrueFalse
      ElseIf rdoMultipleChoice.Checked = True Then
         q = New UWPCS3340.MultiChoice
      ElseIf rdoComment.Checked = True Then
         q = New UWPCS3340.Comment
      End If

      ' Add question to Array
      questions.Add(q)

      AddHandler q.Changed, AddressOf changedHandler

      ' Assign the index as the count of the arraylist
      q.Index = questions.Count

      ' Determins the location of the UserControl
      Dim x As Integer = (Me.ClientSize.Width / 2) - (q.width / 2)
      Dim y As Integer = Me.ClientSize.Height

      ' Display the question
      q.Display(New Point(x, y), Me, UWPCS3340.QuestionMode.Creating)

      setScrollButtons()

   End Sub

   ''' <summary>
   ''' Rmoves the current user control.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
               Handles cmdRemove.Click

      Dim index As Integer
      Dim oldIndex = q.Index

      Try

         q.Remove(Me)
         questions.Remove(q)

      Catch ex As Exception

         MsgBox("No questions to remove!")

      End Try

      If questions.Count > 0 Then

         cmdBack.Enabled = True
         cmdNew.Enabled = True
         cmdSave.Enabled = False
         cmdRemove.Enabled = True

         For index = q.Index To questions.Count

            q = questions(index - 1)
            q.Index = q.Index - 1

         Next

         If oldIndex = questions.Count + 1 Then

            oldIndex = questions.Count

         End If

         q = questions(oldIndex - 1)

         Dim x As Integer = (Me.ClientSize.Width / 2) - (q.width / 2)
         Dim y As Integer = Me.ClientSize.Height

         q.Display(New Point(x, y), Me, UWPCS3340.QuestionMode.Creating)

      Else

         cmdBack.Enabled = True
         cmdNew.Enabled = True
         cmdSave.Enabled = False
         cmdRemove.Enabled = False

      End If

      setScrollButtons()

   End Sub

   ''' <summary>
   ''' Sets the scroll buttons depending on the location and question array size.
   ''' </summary>
   ''' <remarks></remarks>
   Public Sub setScrollButtons()

      If cmdSave.Enabled = False And questions.Count - 1 > 0 Then

         Dim index As Integer = q.Index - 1

         cmdUp.Enabled = True
         cmdDown.Enabled = True
         cmdNext.Enabled = True
         cmdPrevious.Enabled = True


         If index = 0 Then                         'When index is first

            cmdUp.Enabled = False
            cmdPrevious.Enabled = False

         ElseIf index = questions.Count - 1 Then   'When index is last

            cmdNext.Enabled = False
            cmdDown.Enabled = False

         End If

      Else

         cmdUp.Enabled = False
         cmdDown.Enabled = False
         cmdNext.Enabled = False
         cmdPrevious.Enabled = False

      End If

   End Sub

   ''' <summary>
   ''' Saves the user control to the array.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click

      Try

         q.Save()

         cmdBack.Enabled = True
         cmdNew.Enabled = True
         cmdSave.Enabled = False
         cmdRemove.Enabled = True

         setScrollButtons()

      Catch ex As Exception

         MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Prog6")

      End Try

   End Sub

   ''' <summary>
   ''' Moves the element up the array one spot.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub cmdUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUp.Click

      swap(questions.Item(q.Index - 1), questions.Item(q.Index - 2))

      q.Remove(Me)

      q = questions(q.Index - 1)

      Dim x As Integer = (Me.ClientSize.Width / 2) - (q.width / 2)
      Dim y As Integer = Me.ClientSize.Height

      q.Display(New Point(x, y), Me, UWPCS3340.QuestionMode.Creating)

      setScrollButtons()

   End Sub

   ''' <summary>
   ''' Moves the element down the array one spot.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub cmdDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDown.Click

      swap(questions.Item(q.Index), questions.Item(q.Index - 1))

      q.Remove(Me)

      q = questions(q.Index - 1)

      Dim x As Integer = (Me.ClientSize.Width / 2) - (q.width / 2)
      Dim y As Integer = Me.ClientSize.Height

      q.Display(New Point(x, y), Me, UWPCS3340.QuestionMode.Creating)

      setScrollButtons()

   End Sub

   ''' <summary>
   ''' Swaps the two elements.
   ''' </summary>
   ''' <param name="toMove">The element being moved.</param>
   ''' <param name="toReplace">The element that toMove is replacing.</param>
   ''' <remarks></remarks>
   Private Sub swap(ByRef toMove As UWPCS3340.IQuestion, ByRef toReplace As UWPCS3340.IQuestion)

      toMove.Index -= 1
      toReplace.Index += 1

      Dim temp As UWPCS3340.IQuestion
      temp = toMove
      toMove = toReplace
      toReplace = temp

   End Sub

   ''' <summary>
   ''' Cycles to the previous user control in the array.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub cmdPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
               Handles cmdPrevious.Click

      Dim index As Integer = q.Index - 1

      q.Remove(Me)

      q = questions(index - 1)

      Dim x As Integer = (Me.ClientSize.Width / 2) - (q.width / 2)
      Dim y As Integer = Me.ClientSize.Height

      q.Display(New Point(x, y), Me, UWPCS3340.QuestionMode.Creating)

      setScrollButtons()

   End Sub

   ''' <summary>
   ''' Cycles to the next user control in the array.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
               Handles cmdNext.Click

      Dim index As Integer = q.Index - 1

      q.Remove(Me)

      q = questions(index + 1)

      Dim x As Integer = (Me.ClientSize.Width / 2) - (q.width / 2)
      Dim y As Integer = Me.ClientSize.Height

      q.Display(New Point(x, y), Me, UWPCS3340.QuestionMode.Creating)

      setScrollButtons()

   End Sub

   ''' <summary>
   ''' Called when the user edits a currently saved user control.
   ''' </summary>
   ''' <remarks></remarks>
   Private Sub changedHandler()

      cmdBack.Enabled = False
      cmdNew.Enabled = False
      cmdSave.Enabled = True
      cmdRemove.Enabled = True

      setScrollButtons()

   End Sub

   ''' <summary>
   ''' Called when editing the quiz.
   ''' </summary>
   ''' <remarks></remarks>
   Public Sub editQuiz()

      If questions.Count > 0 Then

         Dim x As Integer = (Me.ClientSize.Width / 2) - (q.width / 2)
         Dim y As Integer = Me.ClientSize.Height

         ' Display the question
         q = questions(0)

         q.Display(New Point(x, y), Me, UWPCS3340.QuestionMode.Creating)

         If questions.Count > 1 Then

            cmdUp.Enabled = False
            cmdPrevious.Enabled = False
            cmdNext.Enabled = True
            cmdDown.Enabled = True

         End If

      End If

   End Sub

End Class